export default {
    data() {
        return {
            paging: {
                curPage: 1,
                pageSize: 20,
                total: 0
            },
            listCondition: [],
            otherCondition: [],
            templateId: "",
            tableData: {},
            isShowEdit: false,
            mm: {},
            multipleSelection: [],
            rules: {},
            defaultMM: {},
            ids: []
        }
    },
    
    methods: {
        changeSelect(rows) {
            if (rows) {
                this.ids = rows.map(item => item.id);
            }
        },
        doBatchDel() {
            if (this.ids && this.ids.length > 0) {
                this.$sys.delbatch(this.model, this.table, {
                    ids: this.ids.join(",")
                }).then(res => {
                    if (200 == res.code) {
                        this.search(1);
                        this.$message.success("批量删除成功")
                    } else {
                        this.$message.error("批量删除失败")
                    }
                });
            } else {
                this.$message.info("请选择要删除的数据")
            }
            
        },
        changeSize(size) {
            this.paging.pageSize = size;
            this.search(1);
        },
        search(page, conditions) {
            if (page) {
                this.paging.curPage = page;
            }
            if (!conditions) {
                conditions = [...this.listCondition, ...this.otherCondition];
            }
            this.$sys.templateQuery({
                paging: this.paging,
                listCondition: conditions,
                templateId: this.templateId
            }).then(res => {
                this.tableData = res.data;
                this.paging.total = res.data.paging.total;
            })
        },
        showCopyRow(row) {
            this.$sys.getById(this.model, this.table, {id: row.id}).then(res => {
                this.mm = res.data;
                this.mm.id = "";
                this.isShowEdit = true;
            })
        },
        doSave() {
            if (this.$refs.inForm) {
                this.$refs.inForm.validate((valid) => {
                    if (valid) {
                        this.$sys.save(this.model, this.table, this.mm).then(res => {
                            if (200 == res.code) {
                                this.isShowEdit = false;
                                this.search(1);
                                this.$message.success("保存成功")
                            } else {
                                this.$message.error(res.msg);
                            }
                        });
                    }
                });
            } else {
                this.$sys.save(this.model, this.table, this.mm).then(res => {
                    if (200 == res.code) {
                        this.isShowEdit = false;
                        this.search(1);
                        this.$message.success("保存成功")
                    } else {
                        this.$message.error(res.msg);
                    }
                });
            }
            
            
        },
        showEdit(row) {
            if (row) {
                //修改
                this.$sys.getById(this.model, this.table, {id: row.id}).then(res => {
                    this.mm = res.data;
                    this.isShowEdit = true;
                })
            } else {
                //新增
                this.mm = Object.assign({}, this.defaultMM);
                this.isShowEdit = true;
            }
            this.$sys.getCheckRules(this.model, this.table, {}).then(res => {
                if (200 == res.code) {
                    this.rules = this.$u.convRules(res.data);
                }
            })
        },
        doDel(row) {
            this.$u.confirm().then(res => {
                this.$sys.logicDel(this.model, this.table, {id: row.id}).then(res => {
                    if (200 == res.code) {
                        this.$message.success("删除成功")
                        this.search();
                    } else {
                        this.$messgae.error("删除失败")
                    }
                })
            }).catch(e => {
                this.$message.info("取消删除")
            })
        }
    }
}